package com.example.springbootdemo.suanfa;

public class ZuiChangHuiWen {
    public static void main(String[] args) {
        System.out.println(log("aba"));
        System.out.println("aba".substring(-1, 0));
    }

    public static String log(String s) {
        int max = 0;
        int len = 1;
        int index = 0;
        for (int i = 0; i < s.length(); i++) {
            int l = i - 1;
            int r = i + 1;
            while (l >= 0 && s.charAt(i) == s.charAt(l)) {
                // 左边等于当前
                len++;
                l--;
            }
            while (r < s.length() && s.charAt(i) == s.charAt(r)) {
                // 右边等于当前
                len++;
                r++;
            }
            while (r < s.length() && l >= 0 && s.charAt(l) == s.charAt(r)) {
                // l gen r 相同
                // 注意，此时l 就是初始位置
                len += 2;
                l--;
                r++;
            }
            if (max < len) {
                // 出现更大的回文数字
                max = len;
                index = l + 1;
            }
            len = 1;
        }

        return s.substring(index, index+max);
    }
}
